//
//  GesturePasswordViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/2.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "GesturePasswordViewController.h"
#import "KMNineBoxView.h"
#import "KMUIKitMacro.h"
@interface GesturePasswordViewController ()<KMNineBoxViewDelegate>
@property (strong, nonatomic)KMNineBoxView *nineBoxView;
/**  手势密码  */
@property (strong, nonatomic) NSString  *guestureString;
/**  判断是否是第一次输入  */
@property (assign, nonatomic) BOOL   firstShuRu;

@end

@implementation GesturePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor themeGrayColor];
   self.guestureString =  [[NSUserDefaults standardUserDefaults] objectForKey:@"gesture"];
    if (self.guestureString.length == 0) {
        self.titlelabel.text = @"设置手势密码";
    }else{
        self.titlelabel.text = @"修改手势密码";
    }
    
    [self.view addSubview:self.nineBoxView];
}



#pragma mark - KMNineBoxViewDelegate
- (void)nineBoxDidFinishWithState:(KMNineBoxState)state passSequence:(NSString *)passSequence{
    
    switch (state) {
        case KMNineBoxStatePassed: {

            if (self.guestureString.length == 0) {
                
                self.nineBoxView.isFirst = NO;
                self.nineBoxView.predefinedPassSeq = passSequence;
                if (self.firstShuRu == NO) {
                     [YJProgressHUD showMessage:@"请再次输入验证手势密码" inView:self.view];
                    self.firstShuRu = YES;
                }else{
                    [[NSUserDefaults standardUserDefaults] setObject:passSequence forKey:@"gesture"];
                    [YJProgressHUD showMessage:@"手势密码设置成功" inView:self.view];
                    [self.navigationController popViewControllerAnimated:YES];
                }
               
            }else{
                
               
                    self.nineBoxView.isFirst = NO;
                    self.nineBoxView.predefinedPassSeq = passSequence;
                    if (self.firstShuRu == NO) {
                        [YJProgressHUD showMessage:@"请再次输入验证手势密码" inView:self.view];
                        self.firstShuRu = YES;
                    }else{
                        [[NSUserDefaults standardUserDefaults] setObject:passSequence forKey:@"gesture"];
                        [YJProgressHUD showMessage:@"手势密码修改成功" inView:self.view];
                        [self.navigationController popViewControllerAnimated:YES];
                    }

            }
           
            
            break;
        }
            
        case KMNineBoxStateFailed: {
            if ([self.guestureString isEqualToString:passSequence]) {
                 [YJProgressHUD showMessage:@"原密码和新密码一致,请重新设置" inView:self.view];
                return;
            }
                [YJProgressHUD showMessage:@"两次的密码不一致" inView:self.view];
            
            break;
        }
            
        default:
            break;
    }


}


- (KMNineBoxView *)nineBoxView{
    if (!_nineBoxView) {
        _nineBoxView = [[KMNineBoxView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_WIDTH)];
         _nineBoxView.center = self.view.center;
        _nineBoxView.delegate = self;
        _nineBoxView.isFirst = YES;
        _nineBoxView.predefinedPassSeq = self.guestureString;


    }
    return _nineBoxView;

}


@end
